﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KyleLib.Controls;
using System.Threading;
namespace LinkViewer
{
    /// <summary>
    /// Interaction logic for winLinkViewer.xaml
    /// </summary>
    public partial class winLinkViewer : Window
    {
        LinkWrapper _links;

        //this will currently break if this number is changed (other values/modifier hard coded to 5)
        int _linksPerPage = 10;
        public winLinkViewer(LinkWrapper links)
        {
            InitializeComponent();
            _links = links;

            //Create dummy object for when we have no link to display (less than 5 links total)
            Link_Viewer_Items.DummyObjectLink dummyLink = new LinkViewer.Link_Viewer_Items.DummyObjectLink();


            if (_links.Count > 0)
                linkItem1.LinkObject = links[0];
            else
                linkItem1.LinkObject = dummyLink;

            if (_links.Count > 1)
                linkItem2.LinkObject = links[1];
            else
                linkItem2.LinkObject = dummyLink;

            if (_links.Count > 2)
                linkItem3.LinkObject = links[2];
            else
                linkItem3.LinkObject = dummyLink;

            if (_links.Count > 3)
                linkItem4.LinkObject = links[3];
            else
                linkItem4.LinkObject = dummyLink;

            if (_links.Count > 4)
                linkItem5.LinkObject = links[4];
            else
                linkItem5.LinkObject = dummyLink;

            if (_links.Count > 5)
                linkItem6.LinkObject = links[5];
            else
                linkItem6.LinkObject = dummyLink;
            
            if (_links.Count > 6)
                linkItem7.LinkObject = links[6];
            else
                linkItem8.LinkObject = dummyLink;
            
            if (_links.Count > 7)
                linkItem8.LinkObject = links[7];
            else
                linkItem8.LinkObject = dummyLink;
            
            if (_links.Count > 8)
                linkItem9.LinkObject = links[8];
            else
                linkItem9.LinkObject = dummyLink;
            
            if (_links.Count > 9)
                linkItem10.LinkObject = links[9];
            else
                linkItem10.LinkObject = dummyLink;

            //construct combo box
            MakePageChangeComboBox();

            //add total to top
            lblTotalLinks.Content = links.Count.ToString();
            
            string folderPath = "";
            try
            {
                folderPath = jcc.StandardWebService.WebService.GetFolderPath(jcc.StandardWebService.GetKey(), jcc.StandardWebService.WebService.GetPage(jcc.StandardWebService.GetKey(), links[0].Page_ID).Folder_ID);
            }
            catch
            { }
            lblFolderPath.Content = folderPath;

            
            
        }

        static Thread thread = null;
        public static void LaunchLoader()
        {
            thread = new Thread(new ThreadStart(CreateLoader));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
        public static void KillLoader()
        {
            try
            {
                thread.Abort();
            }
            catch
            { }
        }

        private static void CreateLoader()
        {
            JetNettEZEditor.Loading wpfWindow = new JetNettEZEditor.Loading();
            wpfWindow.Show();
            System.Windows.Threading.Dispatcher.Run();

        }

        private void MakePageChangeComboBox()
        {
            cbPages.Items.Clear();
            int i = _links.Count / 10;
            if (_links.Count % 5 > 0)
                i++;

            //start with page 1 instead of page 0
            for (int j = 1; j <= i; j++)
            {
                cbPages.Items.Add(string.Format("Page {0}", j));
            }

        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            int linkID = (int)e.Parameter;
            MessageBox.Show("Link ID=" + linkID);
        }

 

        public void LoadPage(int pageNumber)
        {
            LaunchLoader();


            App.ResetIdleTimer();
            //minus one from the page number to make it 0 based
            pageNumber--;

            //load the links
            int index = pageNumber * _linksPerPage;

            DateTime startTime = DateTime.Now;
            //for all 5 controls check if there is a link avialable, then load it
            if (_links.Count > index)
            {
                linkItem1.Visibility = System.Windows.Visibility.Visible;
                linkItem1.LinkObject = _links[index];
                index++;
            }
            else
            {
                linkItem1.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (_links.Count > index)
            {
                linkItem2.Visibility = System.Windows.Visibility.Visible;
                linkItem2.LinkObject = _links[index];
                index++;
            }
            else
            {
                linkItem2.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (_links.Count > index)
            {
                linkItem3.Visibility = System.Windows.Visibility.Visible;
                linkItem3.LinkObject = _links[index];
                index++;
            }
            else
            {
                linkItem3.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (_links.Count > index)
            {
                linkItem4.Visibility = System.Windows.Visibility.Visible;
                linkItem4.LinkObject = _links[index];
                index++;
            }
            else
            {
                linkItem4.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (_links.Count > index)
            {
                linkItem5.Visibility = System.Windows.Visibility.Visible;
                linkItem5.LinkObject = _links[index];
                index++;

            }
            else
            {
                linkItem5.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (_links.Count > index)
            {
                linkItem6.Visibility = System.Windows.Visibility.Visible;
                linkItem6.LinkObject = _links[index];
                index++;

            }
            else
            {
                linkItem6.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (_links.Count > index)
            {

                linkItem7.Visibility = System.Windows.Visibility.Visible;
                linkItem7.LinkObject = _links[index];
                index++;
            }
            else
            {
                linkItem7.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (_links.Count > index)
            {

                linkItem8.Visibility = System.Windows.Visibility.Visible;
                linkItem8.LinkObject = _links[index];
                index++;
            }
            else
            {
                linkItem8.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (_links.Count > index)
            {
                linkItem9.Visibility = System.Windows.Visibility.Visible;
                linkItem9.LinkObject = _links[index];
                index++;

            }
            else
            {
                linkItem9.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (_links.Count > index)
            {

                linkItem10.Visibility = System.Windows.Visibility.Visible;
                linkItem10.LinkObject = _links[index];
                index++;

            }
            else
            {
                linkItem10.Visibility = System.Windows.Visibility.Collapsed;
            }
            TimeSpan runTime = DateTime.Now - startTime;
            //MessageBox.Show("Time elapsed: " + Convert.ToString(runTime.TotalMilliseconds / 1000));
            KillLoader();

        }
        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            sviewer.ScrollToTop();
  
            try
            {
                string item = cbPages.SelectedItem.ToString();
                item = item.Remove(0, "Page ".Length);

                int pageNumber = int.Parse(item);

                LoadPage(pageNumber);
                
            }
            catch
            {
                MessageBox.Show("Error somewhere");
            
            }


            
        }

        private bool executeChange = true;
        private void bNextPage_Click(object sender, RoutedEventArgs e)
        {
          
            string item = cbPages.SelectedItem.ToString();
            item = item.Remove(0, "Page ".Length);

            int pageNumber = int.Parse(item);

            pageNumber = pageNumber + 1;
            if (pageNumber <= cbPages.Items.Count)
                cbPages.SelectedItem = string.Format("Page {0}", pageNumber);
          
           
        }

        private void bPrevPage_Click(object sender, RoutedEventArgs e)
        {
         

            //just update the combo box to trigger the page change
            try
            {
                string item = cbPages.SelectedItem.ToString();
                item = item.Remove(0, "Page ".Length);

                int pageNumber = int.Parse(item);

                pageNumber = pageNumber - 1;
                if (pageNumber > 0)
                    cbPages.SelectedItem = string.Format("Page {0}", pageNumber);


            }
            catch
            { }
            
        }

  


        private void bBrowserSizeUpdate_Click(object sender, RoutedEventArgs e)
        {
            int size = 500;
            try
            {
                size = int.Parse(tbBrowserSize.Text);
            }
            catch
            {
                size = 500;
                tbBrowserSize.Text = "" + 500;
            }

            LinkViewer.Properties.Settings.Default.BrowserSize = size;
            LinkViewer.Properties.Settings.Default.Save();


        }

        private void bJump_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show("Height:" + sviewer.VerticalOffset + " Second Height: " + sviewer.ExtentHeight);

            int off = (Convert.ToInt32(sviewer.ExtentHeight) + (10)) / 10;
            sviewer.ScrollToVerticalOffset(sviewer.VerticalOffset + off);
        }

        private void bRefreshLinks_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
            //System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
            //Restart Application
          //  LinkViewer.Properties.Settings.Default.DirectoryIDToOpen = App.LastFolderOpened;
          //  LinkViewer.Properties.Settings.Default.OpenDirectory = true;
           // //LinkViewer.Properties.Settings.Default.Save();

          //  App.EmulateRestart();
         //   this.Close();            
        }

        private void bAreaCodeLookup_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.bennetyee.org/ucsd-pages/area.html");
        }
    }
}
